﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class CustomizeReportBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICustomizeReportDA CustomizeReport = DataAccess.CreateCustomizeReportDA();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public CustomizeReportBLL()
        {
            this.pubParasInfo = null;
        }

        public CustomizeReportBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CustomizeReportBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public DataSet GetLPSaleReport(LPSaleReportQueryEntity lPSaleReportQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(lPSaleReportQueryEntity.OWNER_ID))
            {
                sqlWhere.Add(" A.OWNER_ID " + lPSaleReportQueryEntity.OWNER_ID);
            }
            if (!string.IsNullOrEmpty(lPSaleReportQueryEntity.SKU_ID))
            {
                sqlWhere.Add(" A.SKU_ID " + lPSaleReportQueryEntity.SKU_ID);
            }
            if (!string.IsNullOrEmpty(lPSaleReportQueryEntity.DESCR))
            {
                sqlWhere.Add(" A.DESCR " + lPSaleReportQueryEntity.DESCR);
            }
            if (!string.IsNullOrEmpty(lPSaleReportQueryEntity.SKU_CLASS_ID))
            {
                sqlWhere.Add(" A.SKU_CLASS_ID " + lPSaleReportQueryEntity.SKU_CLASS_ID);
            }
            return CustomizeReport.GetLPSaleReport(sqlWhere, lPSaleReportQueryEntity, this.pubParasInfo, this.dataBaseID[0]);
        }

        public DataSet GetNSNPerformanceReportByDataSet(NSNPerformanceReportQueryEntity nsnPerformanceReportQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.RECEIPT_ID))
            {
                sqlWhere.Add("R.RECEIPT_ID " + nsnPerformanceReportQueryEntity.RECEIPT_ID);
            }
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.VENDOR_CITY))
            {
                sqlWhere.Add(" R.VENDOR_CITY " + nsnPerformanceReportQueryEntity.VENDOR_CITY);
            }
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.VENDOR_NAME))
            {
                sqlWhere.Add(" R.VENDOR_ID " + nsnPerformanceReportQueryEntity.VENDOR_NAME);
            }
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.SKU_ID))
            {
                sqlWhere.Add(" RD.SKU_ID " + nsnPerformanceReportQueryEntity.SKU_ID);
            }
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.EXTERNAL_LOT))
            {
                sqlWhere.Add(" L.EXTERNAL_LOT " + nsnPerformanceReportQueryEntity.EXTERNAL_LOT);
            }
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.CREATED_DATE))
            {
                sqlWhere.Add(nsnPerformanceReportQueryEntity.CREATED_DATE.Replace("@MOVEMENT_DATE@", " M.MOVEMENT_DATE "));
            }
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.UDF10))
            {
                sqlWhere.Add(" M.UDF10 " + nsnPerformanceReportQueryEntity.UDF10);
            }
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.LOT_ATTR01))
            {
                sqlWhere.Add(" L2.LOT_ATTR01 " + nsnPerformanceReportQueryEntity.LOT_ATTR01);
            }
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.START_DATETIME))
            {
                sqlWhere.Add(" R.CREATED_DATE >='" + nsnPerformanceReportQueryEntity.START_DATETIME + "'");
            }
            if (!string.IsNullOrEmpty(nsnPerformanceReportQueryEntity.END_DATETIME))
            {
                sqlWhere.Add("  R.CREATED_DATE <='" + nsnPerformanceReportQueryEntity.END_DATETIME + "'");
            }
            return CustomizeReport.GetNSNPerformanceReportByDataSet(sqlWhere, nsnPerformanceReportQueryEntity, this.pubParasInfo, this.dataBaseID[0]);
        }

        public DataSet GetTNTDailyInventoryRptDataSet(InventoryQueryEntity inventoryQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(inventoryQueryEntity.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + inventoryQueryEntity.OwnerID);
            }
            if (!string.IsNullOrEmpty(inventoryQueryEntity.SkuClassDescr))
            {
                sqlWhere.Add(" SKU_CLASS_NAME " + inventoryQueryEntity.SkuClassDescr);
            }
            if (!string.IsNullOrEmpty(inventoryQueryEntity.Sku))
            {
                sqlWhere.Add(" SKU_ID " + inventoryQueryEntity.Sku);
            }
            if (!string.IsNullOrEmpty(inventoryQueryEntity.Descr))
            {
                sqlWhere.Add(" SKU_DESCR " + inventoryQueryEntity.Descr);
            }
            if (!string.IsNullOrEmpty(inventoryQueryEntity.SkuGroup))
            {
                sqlWhere.Add(" SKU_GROUP " + inventoryQueryEntity.SkuGroup);
            }
            if (!string.IsNullOrEmpty(inventoryQueryEntity.QTY))
            {
                sqlWhere.Add(" QTY " + inventoryQueryEntity.QTY);
            }
            if (!string.IsNullOrEmpty(inventoryQueryEntity.ExpiryDate))
            {
                sqlWhere.Add(inventoryQueryEntity.ExpiryDate.Replace("@ExpiryDate@", " LA.EXPIRY_DATE "));
            }
            return CustomizeReport.GetTNTDailyInventoryRptDataSet(sqlWhere, inventoryQueryEntity, this.pubParasInfo, this.dataBaseID[0]);
        }

        public DataSet GetTNTInOutStockInvRptDataSet(TNTInOutStockInvRptQueryEntity tntInOutStockInvRptQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(tntInOutStockInvRptQueryEntity.OWNER_ID))
            {
                sqlWhere.Add("A.OWNER_ID " + tntInOutStockInvRptQueryEntity.OWNER_ID);
            }
            if (!string.IsNullOrEmpty(tntInOutStockInvRptQueryEntity.SKU_CLASS_ID))
            {
                sqlWhere.Add(" SKU_CLASS_ID " + tntInOutStockInvRptQueryEntity.SKU_CLASS_ID);
            }
            if (!string.IsNullOrEmpty(tntInOutStockInvRptQueryEntity.SKU_ID))
            {
                sqlWhere.Add(" A.SKU_ID " + tntInOutStockInvRptQueryEntity.SKU_ID);
            }
            if (!string.IsNullOrEmpty(tntInOutStockInvRptQueryEntity.DESCR))
            {
                sqlWhere.Add("DESCR " + tntInOutStockInvRptQueryEntity.DESCR);
            }
            if (!string.IsNullOrEmpty(tntInOutStockInvRptQueryEntity.SKU_GROUP))
            {
                sqlWhere.Add(" SKU_GROUP " + tntInOutStockInvRptQueryEntity.SKU_GROUP);
            }
            return CustomizeReport.GetTNTInOutStockInvRptDataSet(sqlWhere, tntInOutStockInvRptQueryEntity, this.pubParasInfo, this.dataBaseID[0]);
        }

        public DataSet GetYITAISerialNoInOutReport(YITAISerialNoInOutReportQueryEntity serialNoInOutReportQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.OWNER_ID))
            {
                sqlWhere.Add(" IT.OWNER_ID " + serialNoInOutReportQueryEntity.OWNER_ID);
            }
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.SKU_ID))
            {
                sqlWhere.Add(" IT.OWNER_ID " + serialNoInOutReportQueryEntity.SKU_ID);
            }
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.EXTERNAL_RECEIPT_ID))
            {
                sqlWhere.Add(" R.EXTERNAL_RECEIPT_ID " + serialNoInOutReportQueryEntity.EXTERNAL_RECEIPT_ID);
            }
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.VENDOR_ID))
            {
                sqlWhere.Add(" R.VENDOR_ID " + serialNoInOutReportQueryEntity.VENDOR_ID);
            }
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.BEGIN_RECEIPT_DATE))
            {
                sqlWhere.Add(" R.RECEIPT_DATE >= '" + serialNoInOutReportQueryEntity.BEGIN_RECEIPT_DATE + "'");
            }
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.END_RECEIPT_DATE))
            {
                sqlWhere.Add(" R.RECEIPT_DATE <= '" + serialNoInOutReportQueryEntity.END_RECEIPT_DATE + "'");
            }
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.EXTERNAL_ORDER_ID))
            {
                sqlWhere.Add(" O.EXTERN_ORDER_ID " + serialNoInOutReportQueryEntity.EXTERNAL_ORDER_ID);
            }
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.CUST_ID))
            {
                sqlWhere.Add(" O.CONSIGNEE_ID " + serialNoInOutReportQueryEntity.CUST_ID);
            }
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.BEGIN_SHIP_DATE))
            {
                sqlWhere.Add(" O.ACTUAL_SHIP_DATE >= '" + serialNoInOutReportQueryEntity.BEGIN_SHIP_DATE + "'");
            }
            if (!string.IsNullOrEmpty(serialNoInOutReportQueryEntity.END_SHIP_DATE))
            {
                sqlWhere.Add(" O.ACTUAL_SHIP_DATE <= '" + serialNoInOutReportQueryEntity.END_SHIP_DATE + "'");
            }
            return CustomizeReport.GetYITAISerialNoInOutReport(sqlWhere, serialNoInOutReportQueryEntity, this.pubParasInfo, this.dataBaseID[0]);
        }

        public string UpdateYITAISerialNoInOutReportFlag(List<string> transIDs, List<string> otTransIDs)
        {
            return CustomizeReport.UpdateYITAISerialNoInOutReportFlag(transIDs, otTransIDs, this.pubParasInfo, this.dataBaseID[0]);
        }
    }
}

